//
//  ChangePasswdViewController.m
//  IntelligentHousehold
//
//  Created by wmwMAC on 17/6/28.
//  Copyright © 2017年 Cassie. All rights reserved.
//

#import "ChangePasswdViewController.h"


#define view_height 60
@interface ChangePasswdViewController ()<UITextFieldDelegate>
{
    NSString *telStr;
    NSString *oldCodeStr;
    NSString *newCodeStr;
    NSString *confirmCodeStr;
}
@property (nonatomic, strong) UIScrollView * scrollView;
@end

@implementation ChangePasswdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = BaseColor;
    self.navigationItem.title = @"修改密码";
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight)];
    self.scrollView.backgroundColor = BaseColor;
//    self.scrollView.contentSize =
    [self.view addSubview:_scrollView];
    
    
    [self addTextUI];
}
-(void)addTextUI{
    
    UIView *telView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, view_height)];
    telView.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:telView];
    UITextField *telTF = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, kUIScreenWidth-20, view_height)];
    telTF.delegate = self;
    telTF.placeholder = @"请输入您的手机号";
    telTF.tag = 2001;
    [telTF addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
    [telView addSubview:telTF];
    
    UIView *oldCodeView = [[UIView alloc] initWithFrame:CGRectMake(0, view_height+1, kUIScreenWidth, view_height)];
    oldCodeView.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:oldCodeView];
    UITextField *oldCodeTF = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, kUIScreenWidth-20, view_height)];
    oldCodeTF.delegate = self;
    oldCodeTF.placeholder = @"请输入您的原始密码";
    oldCodeTF.tag = 2002;
    [oldCodeTF addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
    [oldCodeView addSubview:oldCodeTF];
    
    UIView *newCodeView = [[UIView alloc] initWithFrame:CGRectMake(0, 2*(view_height+1)+10, kUIScreenWidth, view_height)];
    newCodeView.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:newCodeView];
    UITextField *newCodeTF = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, kUIScreenWidth-20, view_height)];
    newCodeTF.delegate = self;
    newCodeTF.placeholder = @"新密码";
    newCodeTF.tag = 2003;
    [newCodeTF addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
    [newCodeView addSubview:newCodeTF];
    
    UIView *confirmView = [[UIView alloc] initWithFrame:CGRectMake(0, 3*(view_height+1)+10, kUIScreenWidth, view_height)];
    confirmView.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:confirmView];
    UITextField *confirmTF = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, kUIScreenWidth-20, view_height)];
    confirmTF.delegate = self;
    confirmTF.tag = 2004;
    confirmTF.placeholder = @"再次确认密码";
    [confirmTF addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
    [confirmView addSubview:confirmTF];
    
    UIButton *confirmBtn = [MyUtil createBtnFrame:CGRectMake(10, CGRectGetMaxY(confirmView.frame)+50, kUIScreenWidth-20, view_height-10) title:@"确认修改" bgImage:@"ann" selectBgImage:nil image:nil target:self action:@selector(confirmAction)];
    [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.scrollView addSubview:confirmBtn];
    
    
    
}
-(void)confirmAction{
    if ([BasePublicMethord isMobileNumber:telStr]) {
        if ([newCodeStr isEqualToString:confirmCodeStr]) {
            //修改密码接口
            
            [self changePassWord];
            
        }else{
            [MBProgressHUD showText:@"两次输入新密码不一致"];
        }
    }else{
        [MBProgressHUD showText:@"手机号有误"];
    }

}

-(void)textChanged:(UITextField *)textField{
    if (textField.tag == 2001) {
        telStr = textField.text;
    }else if (textField.tag == 2002){
        oldCodeStr = textField.text;
    }else if (textField.tag == 2003){
        newCodeStr = textField.text;
    }else{
        confirmCodeStr = textField.text;
    }
    NSLog(@"%@",textField.text);
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    
    
    return YES;
}

-(void)changePassWord{
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    
    NSString *url = [NSString stringWithFormat:XGPWD,RealmName];
    
    
    NSMutableDictionary *parameDic = [NSMutableDictionary dictionary];
    [parameDic setObject:saveDic[@"user_id"] forKey:@"user_id"];
    [parameDic setObject:saveDic[@"token"] forKey:@"token"];
    [parameDic setObject:telStr forKey:@"tell"];
    [parameDic setObject:oldCodeStr forKey:@"pwd"];
    [parameDic setObject:newCodeStr forKey:@"pwd1"];
//    [parameDic setObject:saveDic[@"id"] forKey:@"Id"];

    
//    if ([saveDic[@"id"] isEqualToString:saveDic[@"user_id"]]) {
//        [parameDic setObject:@"0" forKey:@"type"];//用以区分是管理员还是成员 0代表管理员 1代表成员
//    }else{
//        [parameDic setObject:@"" forKey:@"type"];
//    }
    
    [HttpRequest postWithURLString:url parameters:parameDic success:^(id responseObject) {
        
        NSString *msgStr = [responseObject objectForKey:@"msg"];
        if ([msgStr isEqualToString:@"success"]) {
            [MBProgressHUD showText:@"修改成功"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [MBProgressHUD hideHUD];
                
                [self.navigationController popViewControllerAnimated:YES];
            });
            
        }else{
            [MBProgressHUD showText:msgStr];
        }
        
    } failure:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
